package com.dyzx.hbb.modules.attendance.controller;

import com.dyzx.hbb.common.base.Result;
import com.dyzx.hbb.common.config.MyLambdaQueryWrapper;
import com.dyzx.hbb.common.controller.BaseValidController;
import com.dyzx.hbb.modules.attendance.entity.AttendanceDevice;
import com.dyzx.hbb.modules.attendance.query.AttendanceDeviceQuery;
import com.dyzx.hbb.modules.attendance.service.AttendanceDeviceService;
import com.dyzx.hbb.modules.attendance.vo.AttendanceDeviceUVO;
import com.dyzx.hbb.modules.attendance.vo.AttendanceDeviceVO;
import com.dyzx.hbb.security.context.SecurityContextHolder;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/attendance/device")
public class AttendanceDeviceController extends BaseValidController<AttendanceDeviceService, AttendanceDevice, AttendanceDeviceQuery, AttendanceDeviceVO, AttendanceDeviceUVO> {
    @Autowired
    private AttendanceDeviceService service;

    @Override
    protected AttendanceDevice addValid(AttendanceDeviceVO entity) {
        return null;
    }

    @Override
    protected AttendanceDevice editValid(AttendanceDeviceUVO entity) {
        return null;
    }

    @Override
    protected AttendanceDeviceService getBaseService() {
        return service;
    }

    @Override
    protected MyLambdaQueryWrapper<AttendanceDevice> queryWrapper(AttendanceDeviceQuery query) {
        MyLambdaQueryWrapper<AttendanceDevice> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.select(AttendanceDevice::getId, AttendanceDevice::getName, AttendanceDevice::getDescription, AttendanceDevice::getSsid, AttendanceDevice::getIp, AttendanceDevice::getType, AttendanceDevice::getLocation);
        return queryWrapper;
    }

    @Override
    protected MyLambdaQueryWrapper<AttendanceDevice> queryInfoWrapper() {
        MyLambdaQueryWrapper<AttendanceDevice> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.select(AttendanceDevice::getId, AttendanceDevice::getName, AttendanceDevice::getDescription, AttendanceDevice::getSsid, AttendanceDevice::getIp, AttendanceDevice::getType, AttendanceDevice::getLocation);
        return queryWrapper;
    }

    @GetMapping("/mini_list")
    @Operation(summary = "获取考勤配置精简列表", description = "获取所有考勤配置的精简信息，仅包含ID和名称，用于下拉选择")
    public Result<List<Map<String, Object>>> miniList() {
        Long organId = SecurityContextHolder.getOrganId();
        MyLambdaQueryWrapper<AttendanceDevice> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.select(AttendanceDevice::getId).as("value");
        queryWrapper.select(AttendanceDevice::getName).as("label");
        queryWrapper.orderByDesc(AttendanceDevice::getId);
        queryWrapper.eq(AttendanceDevice::getOrganId, organId);
        List<Map<String, Object>> maps = service.listMaps(queryWrapper);
        if (maps.isEmpty()) {
            return Result.error(404, "暂无数据");
        }
        return Result.ok(maps);
    }
}
